$(function () {
        var orderBill_dialog = $("#orderBill_dialog");
        var orderBill_datagrid = $("#orderBill_datagrid");
        var orderBill_form = $("#orderBill_form");
        var pro_datagrid = $("#pro_datagrid");
        var methodObj = {
            add: function () {
                //清空表单数据
                orderBill_form.form("clear");

                $("#edit_tbody").html("");
                //设置标题
                orderBill_dialog.dialog("setTitle", "新增订单");

                //打开弹窗
                orderBill_dialog.dialog("open");
            },

            edit: function () {
                //判断是否有选中数据
                var row = orderBill_datagrid.datagrid("getSelected");
                if (!row) {
                    $.messager.alert('温馨提示', '请选中一条数据!', 'warning');
                    return;
                }
                //清空表单数据
                orderBill_form.form("clear");
                $("#edit_tbody").html("");
                //处理供应商数据
                if (row.supplier) {
                    row["supplier.id"] = row.supplier.id;
                }
                //处理仓库数据
                if (row.depot) {
                    row["depot.id"] = row.depot.id;
                }
                //回显数据
                orderBill_form.form("load", row);
                $.get("/orderbill/getItemsByBillId.do?id="+row.id,function(data){
                    $.each(data, function (index, item) {
                        var copy = $("#itemTr tr:first").clone();

                        copy.appendTo("#edit_tbody");
                        copy.find("[tag=sn]").html("CHTN");
                        //copy.find("[tag=snn]").val(item.product.sn);
                        copy.find("[tag=name]").html(item.product.name);
                        copy.find("[tag=pid]").val(item.product.id);
                        copy.find("[tag=costPrice]").val(item.product.costPrice);
                        copy.find("[tag=amount]").html(item.amount);
                        copy.find("[tag=remark]").val(item.remark);
                        copy.find("[tag=number]").val(item.number);
                    })
                })
                //设置标题
                orderBill_dialog.dialog("setTitle", "编辑订单");
                //打开弹窗
                orderBill_dialog.dialog("open");
            },
            save: function () {
                var id = $("#orderBillId").val();
                var url = "";

                if (id) {
                    url = '/orderbill/update.do';
                } else {
                    url = '/orderbill/save.do';
                }
                $.each($("#edit_tbody tr"), function (index, item) {
                    $(item).find("[tag=pid]").prop("name", "items[" + index + "].product.id");
                    $(item).find("[tag=sn]").prop("name", "items[" + index + "].product.sn");
                    $(item).find("[tag=costPrice]").prop("name", "items[" + index + "].product.costPrice");
                    $(item).find("[tag=number]").prop("name", "items[" + index + "].number");
                    $(item).find("[tag=remark]").prop("name", "items[" + index + "].remark");
                });
                orderBill_form.form("submit", {
                    url: url,
                    success: function (data) {
                        data = $.parseJSON(data);
                        if (data.success) {
                            alert("执行成功")
                            $.messager.alert('温馨提示', '操作成功', 'info', function () {
                                //重新加载数据
                                orderBill_datagrid.datagrid("reload");
                                //关闭弹窗
                                orderBill_dialog.dialog("close");
                            });
                        } else {
                            $.messager.alert('温馨提示', '操作失败', 'error');
                        }
                    }
                });
            },
            removeT: function () {
                orderBill_dialog.dialog("close");
            },
            deletT:function () {
                // 删除
                var row = orderBill_datagrid.datagrid("getSelected");
                if (!row) {
                    $.messager.alert('温馨提示', '请选中一条数据!', 'warning');
                    return;
                }
                var index = orderBill_datagrid.datagrid("getRowIndex",row);
                $.messager.confirm('温馨提示', '确定要删除吗!', function (r) {

                    $.get('orderbill/delete.do?id='+row.id,function (data) {
                        if (data.success){
                        } else{
                            $.messager.alert('温馨提示', data.msg());
                        }
                    })
                    if (r){
                        orderBill_datagrid.datagrid("deleteRow",index);

                        $.messager.alert('温馨提示', '删除成功');
                        orderBill_datagrid.datagrid("reload");
                    } else{
                        $.messager.alert('温馨提示', '删除失败', 'warning');
                    }
                });

            },
            // 审核
           audit: function () {
                //判断是否有选中数据
                var row = orderBill_datagrid.datagrid("getSelected");
                if (!row) {
                    $.messager.alert('温馨提示', '请选中一条数据!', 'warning');
                    return;
                }
                //弹出确认框
                $.messager.confirm('温馨提示', '您想要执行该操作吗?', function (y) {
                    if (y) {
                        $.get("/orderbill/audit.do", {id: row.id}, function (data) {
                            if (data.success) {
                                $.messager.alert('温馨提示', '审核成功', 'info', function () {
                                    orderBill_datagrid.datagrid("reload");
                                });
                            } else {
                                $.messager.alert('温馨提示', "操作失败", 'error');
                            }
                        }, "json")
                    }
                });
            },
            reload: function () {
                orderBill_datagrid.datagrid("reload");
            },

            searchEmployee: function () {
                //获取文本框的内容
                var keyword = $("#keyword").textbox("getValue");
                var firstId = $("#firstId").combobox("getValue");
                var secondId = $("#secondId").combobox("getValue");

                //重新加载数据表格,带上多条件查询参数
                pro_datagrid.datagrid("load", {
                    keyword: keyword,
                    firstId: firstId,
                    secondId: secondId
                })
            },
            addProduct: function () {

                $("#product_view").dialog("open");
            }
        }
        //事件统一绑定
        $("[data-cmd]").click(function () {
            var cmd = $(this).data("cmd");
            methodObj[cmd]();
        });

        orderBill_datagrid.datagrid({
            url: '/orderbill/list.do',
            fit: true,
            fitColumns: true,
            pagination: true,
            singleSelect: true,
            rownumbers: true,
            toolbar: '#orderBill_toolbar',
            columns: [
                [
                    {field: 'sn', title: '订单编号', width: 100},
                    {field: 'totalNumber', title: '订单总数量', width: 100},
                    {field: 'totalAmount', title: '总金额', width: 100},
                    {field: 'vdate', title: '业务时间', width: 100},
                    {field: 'auditTime', title: '审核时间', width: 100},
                    {
                        field: 'inputUser', title: '录入人', width: 100, formatter: function (value) {
                            return value ? value.username : "";
                        }
                    },
                    {
                        field: 'auditor', title: '审核人', width: 100, formatter: function (value) {
                            return value ? value.username : "";
                        }
                    },
                    {
                        field: 'supplier', title: '供应商', width: 100, formatter: function (value) {
                            return value ? value.name : "";
                        }
                    },
                    {
                        field: 'depot', title: '仓库', width: 100, formatter: function (value) {
                            return value ? value.name : ''
                        }
                    },
                    {
                        field: 'status', title: '状态', width: 100, formatter: function (value) {
                            return value == 0 ? '<font color="red">未审核</font>' : '<font color="green">已审核</font>';
                        }
                    },
                    {
                        field: 'operate', title: '操作', width: 100,
                        formatter: function (value, row, index) {
                            if(row.status==1){
                                var str = '<a href="#" name= "opera" class="easyui-linkbutton" style="width:80px"  onclick="add_seve()" ></a>';
                                return str
                            }

                        }
                    }

                ]
            ],

            onLoadSuccess: function () {
                $(".easyui-linkbutton").linkbutton({});

            },

            onLoadSuccess: function (data) {
                $("a[name='opera']").linkbutton({text: '查看', width: 80, iconCls: 'icon-add'});
            },
        });

        pro_datagrid.datagrid({
            url: '/product/listView.do',
            fit: true,
            fitColumns: true,
            pagination: true,
            singleSelect: true,
            rownumbers: true,
            toolbar: '#view_toolbar',
            columns: [[
                {field: 'id', hidden: true},
                {field: 'name', title: '商品名', width: 100},
                {field: 'sn', title: '编码', width: 100},
                {field: 'costPrice', title: '进价', width: 100},
                {field: 'costPrice', title: '售价', width: 100},
                {field: "imgPath", title: "商品图", width: 100, formatter: imgformatter},
                {field: "remark", title: "备注", width: 100}
            ]],
            onClickRow: function (index, row) {
                var copy = $("#itemTr tr:first").clone();
                copy.appendTo("#edit_tbody");
                copy.find("[tag=sn]").html(row.sn);
                copy.find("[tag=snn]").val(row.sn);
                copy.find("[tag=name]").html(row.name);
                copy.find("[tag=pid]").val(row.id);
                copy.find("[tag=costPrice]").val(row.costPrice);
                $("#product_view").dialog("close");
            },

        });
        orderBill_dialog.dialog({
            width: 1000,
            height: 500,
            buttons: "#orderBill_btns",
            closed: true,
            modal: true,
            onOpen: function () {
                $.get('/orderBill/getItemsByBillId.do', function (data) {
                    if (!$("#mySn").textbox("getValue")) {
                        $("#mySn").textbox('setValue', data)
                    }
                })
            },
        });
        $("#product_view").dialog({
            width: 800,
            height: 500,
            closed: true,
            modal: true,
            title: '添加商品'
        });
        $("#edit_tbody").on("change", "[tag=costPrice],[tag=number]", function () {
            var currentTr = $(this).closest('tr');
            var price = currentTr.find("[tag=costPrice]").val() || 0;
            var number = currentTr.find("[tag=number]").val() || 0;
            currentTr.find("[tag=amount]").html((price * number).toFixed(2));
        }).on("click", ".deleteItem", function () {
            var currentTr = $(this).closest("tr");
            currentTr.remove();
        })
    }
)

function add_seve() {
    //判断是否有选中数据
    var row = $("#orderBill_datagrid").datagrid("getSelected");
    if (!row) {
        $.messager.alert('温馨提示', '请选中一条数据!', 'warning');
        return;
    }
    //清空表单数据
    $("#orderBill_form").form("clear");
    $("#edit_tbody").html("");
    //处理供应商数据
    if (row.supplier) {
        row["supplier.id"] = row.supplier.id;
    }
    //处理仓库数据
    if (row.depot) {
        row["depot.id"] = row.depot.id;
    }
    //回显数据
    $("#orderBill_form").form("load", row);
    $.each(row.items, function (index, item) {
        var copy = $("#itemTr tr:first").clone();
        if (row.status) {
            $(":input").prop("readOnly", true);
        } else {
            $(":input").prop("readOnly", false);
        }
        copy.appendTo("#edit_tbody");
        copy.find("[tag=sn]").html("CHTN");
        copy.find("[tag=name]").html(item.product.name);
        copy.find("[tag=pid]").val(item.product.id);
        copy.find("[tag=costPrice]").val(item.product.costPrice);
        copy.find("[tag=amount]").html(item.amount);
        copy.find("[tag=remark]").val(item.remark);
        copy.find("[tag=number]").val(item.number);
    })
    $("#orderBill_dialog").dialog("setTitle", "查看详情");
    //设置标题
    //打开弹窗
    $("#orderBill_dialog").dialog("open");
    if(row.audit){
        $(":input").prop("readOnly",true);
        orderBill_dialog.dialog("setValue","查看明细");
    }else{
        $(":input").prop("readOnly",false);
    }
}

function imgformatter(value, row, index) {
    if (value) {
        return "<img src='" + value + "' style='width: 120px; height: 100px'>";
    }

}